package cn.darkhorse.core.param;

import cn.darkhorse.entity.BpmnWorkflowParam;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yuanjun
 * @since 2020/6/3 16:15
 */
public class ParserStrategy4Flow implements IParamParserStrategy {
    @Override
    public Map<String, Object> parse(BpmnWorkflowParam param) {
        return null;
    }

    @Override
    public <T> Map<String, Object> parse(BpmnWorkflowParam param, Class<T> tClazz, T form,String userName){
        Map<String, Object> resultMap = new HashMap<>(2);

        String paramName = param.getParamName();
        Field[] fields = tClazz.getDeclaredFields();

        // 因为表单定义的名称可能和数据库中的字段一样带下划线，所以需要转换一下再比较
        String paramNameTemp = paramName.replaceAll("_","").toLowerCase();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                String name = field.getName();
                if (paramNameTemp.equals(field.getName().toLowerCase())) {
                  //  BigDecimal value = (BigDecimal)field.get(form);
                    resultMap.put(paramName, field.get(form));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                // todo 需要抛异常
            }
        }

        return resultMap;
    }
    

    
}
